const utils = require("../../../utils/utils");
//获取应用实例
const app = getApp()
Page({
    data: {
        oldSrc: '',
        src: '',
        width: 300, //宽度
        height: 424.2858, //高度
        disable_rotate:false,
        limit_move:false,
        addList: 0,
        isShow: false,
        mengHeight: 0,
        export_scale: 4,
        pdbper: 0,
        temps: [],
        imgUrl: '',
        imgId: 0,
        chooseTempIndex: 0,
        filters: [],
        requestUrl: '',
        appkey: '',
        intoView: 'viewshi',
        index: 0,
        switch1Checked: true,
        mode: 'aspectFill'
    },
    switch1Change(e) {
        this.setData({
            switch1Checked: e.detail.value
        })
    },
    // 修改渲染模式
    modeChange() {
        this.setData({ mode: this.data.mode === 'aspectFit' ? 'aspectFill' : 'aspectFit' });
        this.cropper.imgReset();
    },
    //旋转
    rotate() {
        //在用户旋转的基础上旋转90°
        this.cropper.setAngle(this.cropper.data.angle += 90);
    },
    //重置
    reset() {
        var that = this;
        that.cropper.imgReset();
    },
    //确定
    submitBtn() {
        var that = this;
        // wx.showLoading({
        //     title: '美好值得等待'
        // })
        that.cropper.getImg((obj) => {
            wx.showLoading()
            that.uploadFile(obj.url, '');
        });
    },
    //确定后继续上传
    uploadFile(path) {
        var that = this;
        utils.uploadfile(path, path, '', '', '', function (res) {
            var data = JSON.parse(res.data)
            wx.hideLoading()
            if (data.code == 200) {
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2];
                prevPage.setData({
                    ['list[' + that.data.index + '].printUrl']: data.data.printUrl,
                    ['list[' + that.data.index + '].fileUrl']: data.data.printUrl
                })
                wx.navigateBack();
            } else {
                utils.toast(data.msg);
            }
        })
    },
    onLoad: function (options) {
        var export_scale = 1;
        var query = wx.createSelectorQuery();
        var that = this;
        that.setData({
            export_scale: export_scale
        })
        wx.getSystemInfo({
            success: (result) => {
                var barHeight = result.screenHeight - result.safeArea.bottom + 20
                that.setData({
                    pdbottom: barHeight + 'px',
                    pdbper: barHeight
                })
            },
        })
        that.cropper = that.selectComponent("#image-cropper");
        query.select('#editBottom').boundingClientRect()
        query.exec(function (res) {
            //res就是 所有标签为mjltest的元素的信息 的数组
            //取高度
            that.setData({
                mengHeight: res[0].height,
                pdbper: res[0].height,
            })
        })
        this.setData({
            width: 240,
            img_height: 100,
            // height: 339.42864,
            height: 340,
            disable_ratio: false,
            disable_width: true,
            disable_height: true,
            src: unescape(options.img),
            index: options.index
        })
        wx.setNavigationBarTitle({
            title: '图片编辑',
        })
        // that.cropper.upload();

        this.loadimage();
    },
    loadimage(e) {
        this.cropper.imgReset();
    },
    clickcut(e) {

    },
    end(e) {
        clearInterval(this.data[e.currentTarget.dataset.type]);
    },
})